package com.fangs.lunbotu;

import android.app.Activity;
import android.app.Application;
import android.os.Build;
import android.os.Bundle;
import android.webkit.WebView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.ivans.videocache.HttpProxyCacheServer;


public class Apps extends Application implements Application.ActivityLifecycleCallbacks {

    private static Apps yehApp;
    private HttpProxyCacheServer httpProxyCacheServer;

    @Override
    public void onCreate() {
        super.onCreate();
        yehApp = this;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { //Android 9.0 WebView多进程问题
            String processName = getProcessName();
            if (!getPackageName().equals(processName)) {
                WebView.setDataDirectorySuffix(processName);
            }
        }

        registerActivityLifecycleCallbacks(this);

    }

    private HttpProxyCacheServer newProxy() {
        return new HttpProxyCacheServer.Builder(this)
                .maxCacheSize(1024 * 1024 * 1024)       // 1 Gb for cache
                .fileNameGenerator(new MyFileNameGenerator())
                .build();
    }

    public static void preCacherScreenData(String cacheUrl) {
        PreCacher.getInstance().startCache(cacheUrl, 0);
    }

    public static HttpProxyCacheServer getProxy() {
        Apps app = getInstance();
        if (app.httpProxyCacheServer == null) {
            app.httpProxyCacheServer = app.newProxy();
        }
        return app.httpProxyCacheServer;
    }

    public static Apps getInstance() {
        return yehApp;
    }

    @Override
    public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle bundle) {

    }

    @Override
    public void onActivityStarted(@NonNull Activity activity) {

    }

    @Override
    public void onActivityResumed(@NonNull Activity activity) {

    }

    @Override
    public void onActivityPaused(@NonNull Activity activity) {

    }

    @Override
    public void onActivityStopped(@NonNull Activity activity) {

    }

    @Override
    public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle bundle) {

    }

    @Override
    public void onActivityDestroyed(@NonNull Activity activity) {

    }
}
